package com.fourforfo.fourmall.cart.interceptor;

import com.fourforfo.common.constant.CartConstant;
import com.fourforfo.common.constant.UrlConstant;
import com.fourforfo.common.utils.HttpUtils;
import com.fourforfo.fourmall.cart.vo.UserInfoTO;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.login.SsoWebLoginHelper;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @program: fourmall
 * @description: 购物车拦截器，获取cookie信息判断用户登录状态,没有cookie则设置新的user-key放到cookie中
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-08-23 17:04
 **/
public class CartInterceptor implements HandlerInterceptor {

    //使用ThreadLocal来保存UserInfoTO信息，供Controller快速获取
    public static ThreadLocal<UserInfoTO> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception{

        UserInfoTO userInfoTO = new UserInfoTO();
        /**
         * 从request中获取xxlUser信息
         */
        XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if(xxlUser==null) xxlUser = SsoWebLoginHelper.loginCheck(request, response);
        String redirectUrl = HttpUtils.getRequestURLByForwordedHost(request);
        request.setAttribute(Conf.REDIRECT_URL,redirectUrl);
        if (xxlUser==null){
            //用户没登录

        }else{
            //登录
            userInfoTO.setXxlSsoUser(xxlUser);
        }

        Cookie[] cookies = request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for(Cookie cookie : cookies){
                //判断cookie中是否有user-key
                String name = cookie.getName();
                if(CartConstant.USER_KEY_COOKIE_NAME.equals(name)){
                    userInfoTO.setUserKey(cookie.getValue());
                }
            }
        }

        if(StringUtils.isEmpty(userInfoTO.getUserKey())){
            String userKey = UUID.randomUUID().toString();
            userInfoTO.setUserKey(userKey);
            userInfoTO.setSetedUserKey(true);
        }

        //目标方法执行之前
        threadLocal.set(userInfoTO);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        UserInfoTO userInfoTO = threadLocal.get();
        if(userInfoTO.isSetedUserKey()){
            String userKey = userInfoTO.getUserKey();

            Cookie userKeyCookie = new Cookie(CartConstant.USER_KEY_COOKIE_NAME,userKey);
            userKeyCookie.setDomain(UrlConstant.FOURMALL_URL);
            userKeyCookie.setMaxAge(CartConstant.TEMP_USER_KEY_COOKIE_EXPIRY_TIME);

            response.addCookie(userKeyCookie);
        }

    }
}
